package com.gongj.service;

import com.gongj.entity.User;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


/**
 * 默认BeanName为类名首字母小写  GongjFactoryBean 对应 gongjFactoryBean
 * Spring启动时，会创建类型为GongjFactoryBean的单例Bean(默认单例),然后放入到单例池中(singletonObjects)
 * 然后当调用getBean时，会根据传入的beanName分别返回不同的Bean
 * gongjFactoryBean返回User对象  &gongjFactoryBean返回GongjFactoryBean对象  gongjFactoryBean1(别名)返回User对象
 * 然后将返回的User对象放入FactoryBean的缓冲池中(factoryBeanObjectCache),之后的获取从缓冲池中获取
 */
@Component
@Scope
@Lazy
@DependsOn
@Primary
@Qualifier
public class GongjFactoryBean implements FactoryBean<User> {

	/**
	 * 当该Bean是单例时，该方法只执行一次
	 * @return
	 * @throws Exception
	 */
	@Override
	public User getObject() throws Exception {
		User user = new User();
		System.out.println("====" + user);
		return user;
	}

	@Override
	public Class<?> getObjectType() {
		return User.class;
	}

	/**
	 * 可以控制该Bean的作用域是单例还是原型
	 * @return
	 */
	@Override
	public boolean isSingleton() {
		return false;
	}


}
